// --------------------------------------------------------------------------------------------------------------------
// <copyright company="" file="Cache.cs">
//   
// </copyright>
// <fileheader>
// Name: Cache.cs
// Date: 10/26/2010
// Modified by Vincenzo Panza.
// </fileheader>
// <summary>
//   Base class for cache implementations.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

/* 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace Lucene.Net.Util.Cache
{
    /// <summary>
    /// Base class for cache implementations.
    /// </summary>
    public abstract class Cache
    {
        /// <summary>
        /// Simple Cache wrapper that synchronizes all
        /// calls that access the cache. 
        /// </summary>
        internal class SynchronizedCache_Renamed_Class : Cache
        {
            /// <summary>
            /// The mutex.
            /// </summary>
            internal object mutex;

            /// <summary>
            /// The cache.
            /// </summary>
            internal Cache cache;

            /// <summary>
            /// Initializes a new instance of the <see cref="SynchronizedCache_Renamed_Class"/> class.
            /// </summary>
            /// <param name="cache">
            /// The cache.
            /// </param>
            internal SynchronizedCache_Renamed_Class(Cache cache)
            {
                this.cache = cache;
                this.mutex = this;
            }

            /// <summary>
            /// Initializes a new instance of the <see cref="SynchronizedCache_Renamed_Class"/> class.
            /// </summary>
            /// <param name="cache">
            /// The cache.
            /// </param>
            /// <param name="mutex">
            /// The mutex.
            /// </param>
            internal SynchronizedCache_Renamed_Class(Cache cache, object mutex)
            {
                this.cache = cache;
                this.mutex = mutex;
            }

            /// <summary>
            /// The put.
            /// </summary>
            /// <param name="key">
            /// The key.
            /// </param>
            /// <param name="value_Renamed">
            /// The value_ renamed.
            /// </param>
            public override void Put(object key, object value_Renamed)
            {
                lock (mutex)
                {
                    cache.Put(key, value_Renamed);
                }
            }

            /// <summary>
            /// The get.
            /// </summary>
            /// <param name="key">
            /// The key.
            /// </param>
            /// <returns>
            /// The get.
            /// </returns>
            public override object Get(object key)
            {
                lock (mutex)
                {
                    return cache.Get(key);
                }
            }

            /// <summary>
            /// The contains key.
            /// </summary>
            /// <param name="key">
            /// The key.
            /// </param>
            /// <returns>
            /// The contains key.
            /// </returns>
            public override bool ContainsKey(object key)
            {
                lock (mutex)
                {
                    return cache.ContainsKey(key);
                }
            }

            /// <summary>
            /// The close.
            /// </summary>
            public override void Close()
            {
                lock (mutex)
                {
                    cache.Close();
                }
            }

            /// <summary>
            /// The get synchronized cache.
            /// </summary>
            /// <returns>
            /// </returns>
            internal override Cache GetSynchronizedCache()
            {
                return this;
            }
        }

        /// <summary>
        /// Returns a thread-safe cache backed by the specified cache. 
        /// In order to guarantee thread-safety, all access to the backed cache must
        /// be accomplished through the returned cache.
        /// </summary>
        /// <param name="cache">
        /// The cache.
        /// </param>
        public static Cache SynchronizedCache(Cache cache)
        {
            return cache.GetSynchronizedCache();
        }

        /// <summary>
        /// Called by {@link #SynchronizedCache(Cache)}. This method
        /// returns a {@link SynchronizedCache} instance that wraps
        /// this instance by default and can be overridden to return
        /// e. g. subclasses of {@link SynchronizedCache} or this
        /// in case this cache is already synchronized.
        /// </summary>
        internal virtual Cache GetSynchronizedCache()
        {
            return new SynchronizedCache_Renamed_Class(this);
        }

        /// <summary>
        /// Puts a (key, value)-pair into the cache. 
        /// </summary>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <param name="value_Renamed">
        /// The value_ Renamed.
        /// </param>
        public abstract void Put(object key, object value_Renamed);

        /// <summary>
        /// Returns the value for the given key. 
        /// </summary>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <returns>
        /// The get.
        /// </returns>
        public abstract object Get(object key);

        /// <summary>
        /// Returns whether the given key is in this cache. 
        /// </summary>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <returns>
        /// The contains key.
        /// </returns>
        public abstract bool ContainsKey(object key);

        /// <summary>
        /// Closes the cache.
        /// </summary>
        public abstract void Close();
    }
}